package com.example.yjhuser.service.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yjhuser.mapper.UserMapper;
import com.example.yjhuser.service.UserService;
import com.mysql.jdbc.StringUtils;
import com.yc.yjh.biz.BizException;
import com.yc.yjh.entity.Users;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public Users userLogin(String username, String password) throws BizException {
        if (StringUtils.isNullOrEmpty(username)){
            throw new BizException("用户名不能为空");
        }
        if (StringUtils.isNullOrEmpty(password)){
            throw new BizException("密码不能为空");
        }
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        final Users users = userMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(users)){
            throw new BizException("用户名或密码错误");
        }
        return users;
    }

    @Override
    public int updateUserMsg(Users users) throws BizException {
        if (ObjectUtils.isEmpty(users)){
            throw new BizException("业务异常");
        }
        return userMapper.updateById(users);
    }

    @Override
    public Users getUserById(int id) throws BizException {
        final Users users = userMapper.selectById(id);
        if (ObjectUtils.isEmpty(users)){
            throw new BizException("用户不存在");
        }
        return users;
    }

    @Override
    public Users wxUserLoGIN(Users users) throws BizException {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",users.getOpenid());
        final Users one = userMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(one)){
            throw new BizException("用户未注册,暂未开放注册功能");
        }else {
            return one;
        }
    }

    /**
     * 微信用户登录注册
     * @param users
     * @return
     */
    @Override
    public Users zhuce(Users users) throws BizException {
        if (StringUtils.isNullOrEmpty(users.getUsername()) || StringUtils.isNullOrEmpty(users.getPassword()) || StringUtils.isNullOrEmpty(users.getName())){
            throw new BizException("请正确填写参数");
        }
        QueryWrapper<Users> wrapper  =new QueryWrapper<>();
        wrapper.eq("username",users.getUsername());
        final boolean exists = userMapper.exists(wrapper);
        if (exists){
            throw new BizException("用户名已存在");
        }else{
            int insert = userMapper.insert(users);
            if (insert > 0) {
                QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
                final Users one = userMapper.selectOne(usersQueryWrapper.eq("username", users.getUsername()));
                return one;
            }else{
                throw new BizException("业务异常");
            }

        }
    }
}
